import 'package:flutter/material.dart';

class CustomRoute extends PageRouteBuilder {
  final Widget widget;
  final int routeType;

  //构造方法
  CustomRoute(this.widget, this.routeType)
      : super(
            transitionDuration: Duration(seconds: 1), //过渡时间
            pageBuilder: (
              //构造器
              BuildContext context,
              Animation<double> animation1,
              Animation<double> animation2,
            ) {
              return widget;
            },
            transitionsBuilder: (BuildContext context,
                Animation<double> animation1,
                Animation<double> animation2,
                Widget child) {
              switch (routeType) {
                case 1:
                  return FadeTransition(
                    //渐隐渐现的动画效果
                    opacity:
                        Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                      parent: animation1,
                      curve: Curves.fastOutSlowIn, //动画曲线
                    )),
                    child: child,
                  );
                  break;
                case 2:
                  // 缩放的动画效果
                  return ScaleTransition(
                    scale: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                      parent: animation1,
                      curve: Curves.fastOutSlowIn,
                    )),
                    child: child,
                  );
                  break;
                case 3:
                  // 旋转+缩放动画效果
                  return RotationTransition(
                    turns: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                        parent: animation1, curve: Curves.fastOutSlowIn)),
                    child: ScaleTransition(
                      scale: Tween(begin: 0.0, end: 1.0).animate(
                          CurvedAnimation(
                              parent: animation1, curve: Curves.fastOutSlowIn)),
                      child: child,
                    ),
                  );
                  break;
                case 4:
                  // 左侧滑动进入退出动画
                  return SlideTransition(
                    position: Tween<Offset>(
                            begin: Offset(-1.0, 0.0), end: Offset(0.0, 0.0))
                        .animate(CurvedAnimation(
                            parent: animation1, curve: Curves.fastOutSlowIn)),
                    child: child,
                  );
                  break;
                case 5:
                  // 上侧滑动进入退出动画
                  return SlideTransition(
                    position: Tween<Offset>(
                            begin: Offset(0.0, -1), end: Offset(0.0, 0.0))  
                        .animate(CurvedAnimation(
                            parent: animation1, curve: Curves.fastOutSlowIn)),
                    child: child,
                  );
                  break;
                default:
              }
            });
}

